package com.my.controller;

import com.my.mapper.RoleMapper;
import com.my.pojo.Role;
import com.my.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServlatException;
import javax.servlet.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.rowset.serial.SerialException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebService("/role")
public class RoleController extends HttpServlet {

    protected void doGet(HttpServeltRequest req, HttpServletResponse resp) throws SerialException, IOException{
        PrintWriter out=resp.getWriter();
        req.setCharacterEncoding("utf-8");
        HttpSession Session =req.getSession();
        resp.setContentType("text/html;charset=utf-8");
        String action = req.getParameter("action");
        String result="";
        SqlSession sqlSession =MyBatisUtil.openSessin();
        RoleMapper roleMapper =sqlSession.getMapper(RoleMapper.class);
        if (action.equals("show")){
            List<Role>roles =RoleMapper.searcherAllRole();
            session.setAttribute("roles",roles);
            result="role/showAllRole.jsp";
        }
        if (action.equals("preUpdata")){
            String idStr =req.getParameter("id");
            int id =Integer.parseInt(idStr);
            Role role = roleMapper.searcherRoleByID(id);
            session.setAttribute("role",role);
            result ="role/updataRole.jsp";
        }
        if (action.equals("updata")){
            String idStr = req.getParameter("id");
            int id =Integer.parseInt(idStr);
            String roleName = req.getParameter("roleName");
            String roleDesc =req.getParameter("roleDesc");
            Role role =new Role();
            role.setId(id);
            role.setRoleDesc(roleDesc);
            role.setRoleName(roleName);
            roleMapper.updataRole(role);
            sqlSession.commit();
            result="role?action=show"
        }
        resp.sendRedirect(result);
    }
    @Override
    protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{}
       this.doGet(req,resp);
}
